﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WuDada.Core.Post;
using WuDada.Core.Post.Service;
using WuDada.Core.Post.Domain;

public partial class UserControl_reportLeft : System.Web.UI.UserControl
{
    private PostFactory m_PostFactory;
    private IPostService m_PostService;

    //媒體報導 m_Node = 10
    private readonly int m_Node = 10;
    private int m_PageIndex = 0;
    private readonly int m_PageSize = 7;
    private readonly string m_Path = "report.aspx";

    protected void Page_Load(object sender, EventArgs e)
    {
        m_PostFactory = new PostFactory();
        m_PostService = m_PostFactory.GetPostService();
        m_PageIndex = QueryStringHelper.GetInteger("page", 0);

        if (!IsPostBack)
        {
            FillMenu();
        }
    }

    private void FillMenu()
    {
        lblPageIndex.Text = m_PageIndex.ToString();
        IList<PostVO> list = GetMenuList();
        RepeaterMenu.DataSource = list;
        RepeaterMenu.DataBind();
    }

    private IList<PostVO> GetMenuList()
    {
        Dictionary<string, string> conditions = new Dictionary<string, string>();
        conditions.Add("NodeId", m_Node.ToString());
        conditions.Add("Flag", "1");
        conditions.Add("PageIndex", m_PageIndex.ToString());
        conditions.Add("PageSize", m_PageSize.ToString());
        IList<PostVO> list = m_PostService.GetPostList(conditions);
        return list;
    }

    protected void RepeaterMenu_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemIndex != -1)
        {
            Control ctrl = e.Item;
            PostVO postVO = (PostVO)e.Item.DataItem;
            UIHelper.SetHyperLinkNUrl(ref ctrl, "hlink"
                , string.Format("~/{0}?id={1}&page={2}", m_Path, postVO.PostId, lblPageIndex.Text));
        }
    }

    protected void btnPageUp_Click(object sender, EventArgs e)
    {
        m_PageIndex = int.Parse(lblPageIndex.Text) - 1;
        if (m_PageIndex >= 0)
        {
            PageChanged();
        }
    }    

    protected void btnPageDown_Click(object sender, EventArgs e)
    {
        m_PageIndex = int.Parse(lblPageIndex.Text) + 1;
        if (m_PageIndex >= 0)
        {
            PageChanged();
        }
    }

    private void PageChanged()
    {
        IList<PostVO> list = GetMenuList();                
        if (list != null && list.Count > 0)
        {
            string page = m_PageIndex.ToString();
            string id = list[0].PostId.ToString();
            Response.Redirect(string.Format("~/{0}?id={1}&page={2}", m_Path, id, page));
        }
    }
}